﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using LotteryAdmin.AdminService;
namespace LotteryAdmin
{
    public partial class AddMember : ChildWindow
    {
        LotteryAdminClient srv;
        public string userId;
        public AddMember()
        {
            InitializeComponent();
            srv = WcfProxy.GetProxy;
            srv.GetOverAllCfgCompleted += new EventHandler<GetOverAllCfgCompletedEventArgs>(srv_GetOverAllCfgCompleted);
            srv.AddMemberCompleted += new EventHandler<AddMemberCompletedEventArgs>(srv_AddMemberCompleted);
            this.Loaded += new RoutedEventHandler(AddMember_Loaded);
            this.dataForm1.Loaded += new RoutedEventHandler(dataForm1_Loaded);
        }

        void dataForm1_Loaded(object sender, RoutedEventArgs e)
        {
            (this.dataForm1.FindNameInContent("tbBackPct") as NumericUpDown).Increment = 0.1;
            (this.dataForm1.FindNameInContent("tbBackPct") as NumericUpDown).Maximum = 10;
             //srv.GetOverAllCfgAsync();
        }

        void srv_AddMemberCompleted(object sender, AddMemberCompletedEventArgs e)
        {
            if (e.Result)
            {
                MessageBox.Show("新增会员成功!");
                this.DialogResult = true;
            }
            else
                MessageBox.Show("用户名已经存在");
        }

        void srv_GetOverAllCfgCompleted(object sender, GetOverAllCfgCompletedEventArgs e)
        {
            //if (e.Result.Keys.Contains("MaxBonusPct"))
            //{
            //    (this.dataForm1.FindNameInContent("tbBackPct") as NumericUpDown).Maximum = Convert.ToDouble(e.Result["MaxBonusPct"]);
            //}
            //if (e.Result.Keys.Contains("MemBonusIncrement"))
            //    (this.dataForm1.FindNameInContent("tbBackPct") as NumericUpDown).Increment = Convert.ToDouble(e.Result["MemBonusIncrement"]);
            (this.dataForm1.FindNameInContent("tbBackPct") as NumericUpDown).Increment = 0.1;
        }

        void AddMember_Loaded(object sender, RoutedEventArgs e)
        {
            this.dataForm1.CurrentItem = new User();
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (dataForm1.ValidateItem())
            {
                User newUser = dataForm1.CurrentItem as User;
                if (newUser.UserId.Length > 20)
                {
                    MessageBox.Show("用户名长度不能超过20");
                    return;
                }
                if (!Validate.ValidateUserIDModify(newUser.UserId))
                {
                    MessageBox.Show("用户名只接受数字、汉字、字母、下划线组合");
                    return;
                }
                if (newUser.UserPwd.Length > 20 || newUser.UserPwd.Length < 6)
                {
                    MessageBox.Show("密码长度范围为6-20");
                    return;
                }
                if (!Validate.ValidateInputModify(newUser.UserPwd))
                {
                    MessageBox.Show("密码只接受数字、字母、下划线组合");
                    return;
                }
                //newUser.Bonus = (this.dataForm1.FindNameInContent("tbBackPct") as NumericUpDown).Value;
                newUser.RoleID = Convert.ToInt32(((this.dataForm1.FindNameInContent("cbx_type") as ComboBox).SelectedItem as ComboBoxItem).Tag.ToString());
                userId = newUser.UserId;
                srv.AddMemberAsync(newUser);
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}

